package com.todo.kettle;

import com.todo.kettle.core.PropsUI;
import org.pentaho.di.core.DBCache;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.logging.*;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.job.JobExecutionConfiguration;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
import org.pentaho.di.trans.TransExecutionConfiguration;
import org.pentaho.metastore.stores.delegate.DelegatingMetaStore;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.ResourceBundle;

@Component
public class App implements ApplicationContextAware {

	private static App app;
	public static KettleDatabaseRepositoryMeta meta;

	private LogChannelInterface log;
	private TransExecutionConfiguration transExecutionConfiguration;
	private TransExecutionConfiguration transPreviewExecutionConfiguration;
	private TransExecutionConfiguration transDebugExecutionConfiguration;
	private JobExecutionConfiguration jobExecutionConfiguration;
	public PropsUI props;

	private App() {
		props = PropsUI.getInstance();
		log = new LogChannel( PropsUI.getAppName());
		loadSettings();

		transExecutionConfiguration = new TransExecutionConfiguration();
		transExecutionConfiguration.setGatheringMetrics( true );
		transPreviewExecutionConfiguration = new TransExecutionConfiguration();
		transPreviewExecutionConfiguration.setGatheringMetrics( true );
		transDebugExecutionConfiguration = new TransExecutionConfiguration();
		transDebugExecutionConfiguration.setGatheringMetrics( true );

		jobExecutionConfiguration = new JobExecutionConfiguration();

		variables = new RowMetaAndData( new RowMeta() );
	}

	public void loadSettings() {
		LogLevel logLevel = LogLevel.getLogLevelForCode(props.getLogLevel());
		DefaultLogLevel.setLogLevel(logLevel);
		log.setLogLevel(logLevel);
		KettleLogStore.getAppender().setMaxNrLines(props.getMaxNrLinesInLog());

		// transMeta.setMaxUndo(props.getMaxUndo());
		DBCache.getInstance().setActive(props.useDBCache());
	}

	public static App getInstance() {
		if (app == null) {
			app = new App();
			app.initRepository();
		}
		return app;
	}

	private Repository repository;

	public Repository getRepository() {
		initRepository();
		return repository;
	}

	private Repository defaultRepository;

//	public void initDefault(Repository defaultRepo) {
//		if(this.defaultRepository == null)
//			this.defaultRepository = defaultRepo;
//		this.repository = defaultRepo;
//	}

	public Repository getDefaultRepository() {
		return this.defaultRepository;
	}

	public void selectRepository(Repository repo) {
		if(repository != null) {
			repository.disconnect();
		}
		repository = repo;
	}

	private DelegatingMetaStore metaStore;

	public DelegatingMetaStore getMetaStore() {
		return metaStore;
	}

	public LogChannelInterface getLog() {
		return log;
	}

	private RowMetaAndData variables = null;
	private ArrayList<String> arguments = new ArrayList<String>();

	public String[] getArguments() {
		return arguments.toArray(new String[arguments.size()]);
	}

	public JobExecutionConfiguration getJobExecutionConfiguration() {
		return jobExecutionConfiguration;
	}

	public TransExecutionConfiguration getTransDebugExecutionConfiguration() {
		return transDebugExecutionConfiguration;
	}

	public TransExecutionConfiguration getTransPreviewExecutionConfiguration() {
		return transPreviewExecutionConfiguration;
	}

	public TransExecutionConfiguration getTransExecutionConfiguration() {
		return transExecutionConfiguration;
	}

	public RowMetaAndData getVariables() {
		return variables;
	}


	public final static ResourceBundle bundle = ResourceBundle.getBundle("jdbc");// DBConnectionConfig.properties配置文件的名称
	public final static String HOSTNAME = bundle.getString("jdbc.host");
	public final static String PORT = bundle.getString("jdbc.port");
	public final static String DB = bundle.getString("jdbc.db");
	public final static String USERNAME = bundle.getString("jdbc.username");
	public final static String DBPASS = bundle.getString("jdbc.password");
	public void initRepository(){
		if(repository!=null){return;}
		KettleDatabaseRepository repository = new KettleDatabaseRepository();
		try {
			DatabaseMeta dbMeta = new DatabaseMeta();
//			String hostname = "localhost";
//			String port = "3306";
//			String dbName = "kettle";
			dbMeta.setName(HOSTNAME+"_"+DB);
			dbMeta.setDBName(DB);
			dbMeta.setDatabaseType("MYSQL");
			dbMeta.setAccessType(0);
			dbMeta.setHostname(HOSTNAME);
			dbMeta.setServername(HOSTNAME);
			dbMeta.setDBPort(PORT);
			dbMeta.setUsername(USERNAME);
			dbMeta.setPassword(DBPASS);
			ObjectId objectId = new LongObjectId(100);
			dbMeta.setObjectId(objectId);
			dbMeta.setShared(true);
			dbMeta.addExtraOption(dbMeta.getPluginId(), "characterEncoding", "utf8");
			dbMeta.addExtraOption(dbMeta.getPluginId(), "useUnicode", "true");
			dbMeta.addExtraOption(dbMeta.getPluginId(), "autoReconnect", "true");
			meta = new KettleDatabaseRepositoryMeta();
			meta.setName(HOSTNAME+"_"+DB);
			meta.setId("KettleDatabaseRepository");
			meta.setConnection(dbMeta);
			meta.setDescription(HOSTNAME+"_"+DB);
			repository.init(meta);
			repository.connect("admin", "admin");
			this.repository = repository;
			System.out.println("资源库连接成功");
		} catch (Exception e) {
			System.out.println("资源库连接失败");
		}
	}
	@Override
	public void  setApplicationContext(ApplicationContext context) throws BeansException {
		DataSource dataSource = (DataSource)context.getBean("dataSource");
		if(dataSource!=null){
			System.out.println("连接数据库成功");
			System.out.println("数据库url"+dataSource.toString());
		}

	}


}
